from sqlalchemy import Column, BigInteger, String, DateTime, Date, Enum, Integer, Text, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from datetime import datetime, timezone

Base = declarative_base()

class User(Base):
    __tablename__ = "users"

    id = Column(BigInteger, primary_key=True, autoincrement=True)
    username = Column(String(50), unique=True, nullable=True)
    email = Column(String(100), unique=True, nullable=True)
    phone = Column(String(20), unique=True, nullable=True)
    hashed_password = Column(String(60), nullable=True)
    password = Column(String(20), unique=True, nullable=True)  # 建议删除此字段

    nickname = Column(String(50), nullable=False, default='')
    avatar_url = Column(String(255), nullable=True)

    status = Column(Integer, nullable=False, default=1)  # 状态: 1=正常, 2=禁用
    created_at = Column(DateTime, nullable=True)
    updated_at = Column(DateTime, nullable=True)

    last_login_date = Column(DateTime, nullable=True)
    login_count = Column(Integer, nullable=False, default=0)
    birthday = Column(Date, nullable=True)
    gender = Column(Enum("male", "female", "other"), nullable=True, default="other")
    
    # 关联用户的数据
    user_data = relationship("UserData", back_populates="user")

class UserData(Base):
    __tablename__ = 'user_data'
    
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey('users.id'))
    date = Column(DateTime)
    title = Column(String(255))
    content = Column(Text)  # 用于存储可能的详细内容
    created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc))
    updated_at = Column(DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
    
    # 关联用户
    user = relationship("User", back_populates="user_data")